#include "checkinwindow.h"


//打卡界面可以自行设计，输入品牌和产品名后，可以从数据库获得咖啡因含量，然后进行记录;
//用户可以查看近30天的打卡记录，可以生成最近一周和一个月的平均咖啡因摄入量;
void CheckinWindow::onCheckinButtonClicked()
{
    QString brand = _brandLineEdit->text();
    QString product = _productLineEdit->text();

    if (brand == "" || product == "") {
        MessageBox("错误信息，请重新输入", this).exec();
    }

    double caffeine = SqlHelper::instance().getCaffeine(brand, product);
    if (caffeine == -1.0) {
        MessageBox("未知品牌或产品，请先录入", this).exec();
    } else {
        if (User::instance().getUser() == "")
            qDebug() << "user name is empty, danger bug";

        SqlHelper::instance().insertRecord(User::instance().getUser(), getCurrentTime(), caffeine);
        MessageBox("打卡成功，时间：" + getCurrentTime() + "咖啡因：" + QString::number(caffeine), this).exec();
    }
}

void CheckinWindow::onEnterButtonClicked()
{
    QString brand = _brandLineEdit->text();
    QString product = _productLineEdit->text();
    QString caffeine = _caffeineLineEdit->text();

    if (brand == "" || product == "") {
        MessageBox("错误信息，请重新输入", this).exec();
    }
    else
    {
        MessageBox("录入成功", this).exec();
        SqlHelper::instance().insertProduct(brand, product, caffeine.toDouble());
    }
}

void CheckinWindow::onRecordButtonClicked()
{
    RecordsWindow* w = new RecordsWindow();
    w->show();
}


CheckinWindow::CheckinWindow(QWidget* parent)
    : QWidget(parent)
{
    resize(400, 300);
    showCentered();

    QLabel *brandNameLabel = new QLabel("品牌名:", this);
    QLabel *prodouctNameLabel = new QLabel("产品名:", this);
    QLabel *caffeineNameLabel = new QLabel("摄入量:", this);

    _brandLineEdit = new QLineEdit(this);
    _productLineEdit = new QLineEdit(this);
    _caffeineLineEdit = new QLineEdit(this);
    _caffeineLineEdit->setText("若要打卡，不必输入此项");


    _enterButton = new QPushButton("录入", this);
    _checkinButton = new QPushButton("打卡", this);
    _recordButton = new QPushButton("查看记录", this);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QHBoxLayout *brandNameLayout = new QHBoxLayout;
    QHBoxLayout *caffeineNameLayout = new QHBoxLayout;
    QHBoxLayout *prodouctNameLayout = new QHBoxLayout;

    brandNameLayout->addWidget(brandNameLabel);
    brandNameLayout->addWidget(_brandLineEdit);
    prodouctNameLayout->addWidget(prodouctNameLabel);
    prodouctNameLayout->addWidget(_productLineEdit);
    caffeineNameLayout->addWidget(caffeineNameLabel);
    caffeineNameLayout->addWidget(_caffeineLineEdit);

    mainLayout->addLayout(brandNameLayout);
    mainLayout->addLayout(prodouctNameLayout);
    mainLayout->addLayout(caffeineNameLayout);
    mainLayout->addWidget(_enterButton);
    mainLayout->addWidget(_checkinButton);
    mainLayout->addWidget(_recordButton);

    setLayout(mainLayout);

    connect(_checkinButton, &QPushButton::clicked, this, &CheckinWindow::onCheckinButtonClicked);
    connect(_recordButton, &QPushButton::clicked, this, &CheckinWindow::onRecordButtonClicked);
    connect(_enterButton, &QPushButton::clicked, this, &CheckinWindow::onEnterButtonClicked);
}

void CheckinWindow::showCentered()
{
    if (parentWidget()) {
        QRect parentGeometry = parentWidget()->geometry();
        int x = parentGeometry.x() + (parentGeometry.width() - this->width()) / 2;
        int y = parentGeometry.y() + (parentGeometry.height() - this->height()) / 2;
        this->move(x, y);
    }
}

QString CheckinWindow::getCurrentTime() {

    QDateTime currentDateTime = QDateTime::currentDateTime();

    QString formattedTime = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
    return formattedTime;
}
